<template>
  <el-dialog :title="info.title" :visible.sync="visible" append-to-body center width="760px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="归属单位" prop="company">
            <el-select v-model="form.company" size="small" style="width: 100%">
              <el-option
                v-for="item in companys"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票类别" prop="type">
            <el-select v-model="form.type" size="small" style="width: 100%">
              <el-option
                v-for="item in types"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同编号" prop="pur_id">
            <el-select
              v-model="form.pur_id"
              size="small"
              :remote-method="selPurs"
              remote
              filterable
              style="width: 100%"
              placeholder="请选择合同"
              clearable
              @change="purChange"
              @focus="selPurs"
            >
              <el-option
                v-for="item in purs"
                :key="item.key"
                :label="'LOPO' + item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商" prop="">
            <el-input v-model="sup_name" size="small" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票号码" prop="num">
            <el-input v-model="form.num" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="未税金额" prop="no_tax">
            <el-input v-model="form.no_tax" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税额" prop="tax">
            <el-input v-model="form.tax" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税率" prop="rate">
            <el-input v-model="form.rate" style="width: 90%" size="small" clearable /> %
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开票日期" prop="invoice_time">
            <el-date-picker
              v-model="form.invoice_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收票日期" prop="collect_time">
            <el-date-picker
              v-model="form.collect_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { pur, selPurs, addPurchaseInvoice, updPurchaseInvoice } from '@/api/purchase'
export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      purs: [],
      companys: [{ key: '里欧', value: '里欧' }, { key: '水务分公司', value: '水务分公司' }],
      types: [{ key: '增值税普通发票', value: '增值税普通发票' }, { key: '增值税专用发票', value: '增值税专用发票' }],
      form: {},
      sup_name: '',
      rules: {
        company: [{ required: true, trigger: 'blur', message: '归属单位不可为空' }],
        pur_id: [{ required: true, trigger: 'blur', message: '合同编号不可为空' }],
        no_tax: [{ required: true, trigger: 'blur', message: '未税金额不可为空' }],
        tax: [{ required: true, trigger: 'blur', message: '税额不可为空' }],
        rate: [{ required: true, trigger: 'blur', message: '税率不可为空' }],
        type: [{ required: true, trigger: 'blur', message: '付款项目不可为空' }],
        invoice_time: [{ required: true, trigger: 'blur', message: '开票日期不可为空' }],
        collect_time: [{ required: true, trigger: 'blur', message: '收票日期不可为空' }],
        num: [{ required: true, trigger: 'blur', message: '发票号码不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.sup_name = ''
        this.form = this.info.edit
        if (this.info.type === 'edit') {
          this.pur()
        }
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    purChange() {
      this.pur()
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          if (this.info.type === 'add') {
            addPurchaseInvoice(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updPurchaseInvoice(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    selPurs(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selPurs({ sn: str }).then(response => {
        this.purs = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    pur() {
      this.$set(this, 'purs', [])
      pur({ id: this.form.pur_id }).then(response => {
        const info = {}
        info.key = response.info.id
        info.value = response.info.sn
        this.sup_name = response.info.sup_name
        this.purs.push(info)
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
